//
//  LanguageSettingViewController.m
//  JunZeWristband
//
//  Created by Kendama on 2017/9/12.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "LanguageSettingViewController.h"

@interface LanguageSettingViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property(nonatomic,strong) NSArray *dataSource;
@property(nonatomic,assign) NSInteger selectedIndex;

@end

@implementation LanguageSettingViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)dealloc
{
    
}

- (void)refreshBaseControlValue
{
    if (EXRACT_OBJECT(@"lan"))
    {
        if ([EXRACT_OBJECT(@"lan") isEqualToString:@"zh"])
        {
            self.selectedIndex = 0;
        }
        else
        {
            self.selectedIndex = 1;
        }
    }
    else
    {
        self.selectedIndex = 0;
    }

    
}

#pragma mark - 点击事件
- (void)onLeftButtonClick:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 事件处理

#pragma mark - 代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    //    return self.dataSource.count;
    return 1;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return px1920Height(161);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString * const cellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    }

    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.textColor = [UIColor darkGrayColor];
    cell.textLabel.text = self.dataSource[indexPath.row];
    
    cell.tintColor = [UIColor colorWithHexString:@"F06161"];
    if (self.selectedIndex == indexPath.row)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    //    cell.textLabel.text = self.dataSource[indexPath.row];
    //    cell.textLabel.textColor = [UIColor darkGrayColor];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.selectedIndex = indexPath.row;
    [self.tableView reloadData];
    if (indexPath.row == 0)
    {
        if (![EXRACT_OBJECT(@"lan") isEqualToString:@"zh"])
        {
            SAVE_OBJECT(@"zh", @"lan");
            [[TEALocalizableTool shareInstance] setCurrentLanguage:@"zh-Hans"];
        }
        
    }
    else
    {
        if (![EXRACT_OBJECT(@"lan") isEqualToString:@"en"])
        {
            SAVE_OBJECT(@"en", @"lan");
            [[TEALocalizableTool shareInstance] setCurrentLanguage:@"en"];
        }
       
    }
    [[AppDelegate defaultService] setRootViewController];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark - 通知处理

#pragma mark - 初始化
- (NSArray *)dataSource
{
    if(!_dataSource)
    {
        _dataSource = @[MyNSLocalizedString(@"中文"),MyNSLocalizedString(@"英文")];
    }
    return _dataSource;
}

- (UITableView *)tableView
{
    if(!_tableView)
    {
        UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        //        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //            //            [self getNewsListWithPage:1];
        //        }];
        [superView addSubview:_tableView];
    }
    return _tableView;
}

- (void)setNavigationStyle
{
    [self setNavigationBarTitle:MyNSLocalizedString(@"设置")];
}

- (void)masLayoutSubview
{
    __weak UIView *superView = self.view;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(superView).with.offset(64);
        make.left.right.equalTo(superView);
        make.bottom.equalTo(superView);
    }];
}

@end
